class Hash
  def to_qs
    converted = ""
    self.each{|k,v| converted += "&#{k}=#{CGI.escape(v.to_s)}" unless v.to_s.length == 0}
    return converted
  end
  
  def to_sym
      new_hash = {}
      self.each{|k,v| new_hash[k.to_sym] = v}
      return new_hash
  end
end

class Array
  def to_sym
    self.collect do |e|
      new_hash = {}
      e.each{|k,v| new_hash[k.to_sym] = v}
      e = new_hash
    end
  end
end

class Stikkit::Client
  attr_accessor :api_key, :http
  attr_accessor :name, :text, :tags, :created, :updated, :kind, :page, :raw_text
  
  def initialize(api_key)
    @api_key = api_key
    @http = Net::HTTP.new('api.stikkit.com')
  end
  
  def update(id)
    JSON.parse(self.http.post('/stikkits/' + id.to_s + '.json?api_key=' + self.api_key + '&_method=put&raw_text=' + CGI.escape(self.raw_text), nil).response.body).to_sym
  end
  
  def create
    JSON.parse(self.http.post('/stikkits.json?api_key=' + self.api_key + '&raw_text=' + CGI.escape(self.raw_text), nil).response.body).to_sym
  end
  
  def stikkits
    args = self.defaults.update self.properties
    JSON.parse(self.http.get('/stikkits.json?api_key=' + self.api_key + args.to_qs).response.body).to_sym
  end
  
  def todos
    JSON.parse(self.http.get('/todos.json?api_key=' + self.api_key).response.body)
  end
  
  protected
  
  def properties
    {
      :name => self.name,
      :text => self.text,
      :tags => self.tags,
      :created  => self.created,
      :updated => self.updated,
      :kind => self.kind,
      :page => self.page
    }
  end
  
  def defaults
    {
      :name => "",
      :text => "",
      :tags => "",
      :created  => "",
      :updated => "",
      :kind => "",
      :page => ""
    }
  end
end